<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Resources\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Resources\model\Session;
use Resources\Form\AdditionalForm;
use Resources\model\Additional;
use Zend\View\Model\JsonModel;

class AdditionalController extends AbstractActionController
{
    /*function and variables*/
    protected $form;
    
    protected $authservice;
    
    protected $userinfo;
    
    protected $additionalTable;
    
    private function processData($data) {
        $data['ID']=$this->generateID();
        return $data;
    }
    
    private function generateID(){
        $table_key = '1012';
        $key = $table_key.time();
        return $key;
    }
    
    public function getAuthService()
    {
        if (! $this->authservice) {
            $this->authservice = $this->getServiceLocator()->get('AuthService');
        }
        return $this->authservice;
    }
    
    public function getUserInfo()
    {
        if ($this->getAuthService()->getIdentity()) {
            $this->userinfo = $this->getAuthService()->getStorage()->read();
        }
        return $this->userinfo;
    }
    
    public function getAdditionalTable()
    {
        if(! $this->additionalTable) {
            $this->additionalTable = $this->getServiceLocator()->get('Resource\Model\AdditionalTable');
        }
        return $this->additionalTable;
    }
    
    protected function createAdditional(array $data,$type)
    {
        $additional=new Additional();
        $additional->exchangeArray($data);
        $this->getAdditionalTable()->saveAdditional($additional,$type);
        return true;
    }
    
    /*Actions*/
    public function rolecomboAction() {
         
        if ($this->getAuthService ()->getIdentity ()) {
            if(isset($_POST['textData']) && !($_POST['textData']==0))
            {
                $id = $_POST['textData'];
                $result=$this->getAdditionalTable()->getAdditional($id);
                 
                return new JsonModel([
                    'title'=>$result->DURATION,
                ]);
            }
        }
        return $this->redirect ()->toRoute ( 'Login' );
    }
    
    
    public function indexAction()
    {
        $this->layout()->setTemplate('layout/additionallayout');
        
        if ($this->getAuthService()->getIdentity()) {
            
            $userinfo = $this->getUserInfo();
            $this->layout()->role= $userinfo->ROLE;
            $this->layout()->user=$userinfo->USERNAME;
    
            if (! $userinfo->ROLE == 'admin') {
                return $this->redirect()->toRoute('Login');
            }
            
            $paginator = $this->getAdditionalTable()->fetchAll(true);
            $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
            $paginator->setItemCountPerPage(10);
    
            $viewmodel=new ViewModel(array(
                'paginator' => $paginator
            ));
            return $viewmodel;
        }
        return $this->redirect()->toRoute('Login');
    }
    
    
    public function addAction()
    {
        $this->layout()->setTemplate('layout/additionallayout');
        
        if ($this->getAuthService()->getIdentity()) {
                        
            $userinfo = $this->getUserInfo();
            $this->layout()->role= $userinfo->ROLE;
            $this->layout()->user=$userinfo->USERNAME;

            $form = new AdditionalForm();
            
            $viewmodel=new ViewModel(array(
                'form' => $form,
                'id' => $this->params()->fromRoute('id')
            ));
    
            return $viewmodel;
    
        }else {
            return $this->redirect()->toRoute('Login');
        }
    }
    
    public function processAction()
    {
        $this->layout()->setTemplate('layout/additionallayout');
        
        if ($this->getAuthService()->getIdentity()) {
    
            $userinfo = $this->getUserInfo();
            $this->layout()->role= $userinfo->ROLE;
            $this->layout()->user=$userinfo->USERNAME;
    
            if (! $this->request->isPost()) {
                return $this->redirect()->toRoute(NULL, array(
                    'controller' => 'Additional',
                    'action' => 'add'
                ));
            }
    
            $post = $this->request->getPost();
            $form = new AdditionalForm();
    
            $form->setData($post);
            
            if (! $form->isValid()) {
                $model = new ViewModel(array(
                    'form' => $form,
                    'id' => $this->params()->fromRoute('id')
                ));
                $model->setTemplate('resources/additional/add');
                return $model;
            }
    
            $data=$form->getData();
            
            if(!isset($data['ID'])||$data['ID']==0){
                $data = $this->processData($data);
                $this->createAdditional($data,0);
            }
            else{
                $this->createAdditional($data,1);
            }
    
            return $this->redirect()->toRoute('resources', array('controller'=> 'Additional','action' => 'index'));
    
        }else {
            return $this->redirect()->toRoute('Login');
        }
    }
   
    public function editAction()
    {
        $this->layout()->setTemplate('layout/additionallayout');
        
        if ($this->getAuthService()->getIdentity()) {
            $form = new AdditionalForm();
    
            $userinfo = $this->getUserInfo();
            $this->layout()->role= $userinfo->ROLE;
            $this->layout()->user=$userinfo->USERNAME;
    
            $additional = $this->getAdditionalTable()->getAdditional($this->params()->fromRoute('id'));
            $form->bind($additional);
            $viewModel = new ViewModel(array(
                'form' => $form,
                'id' => $this->params()->fromRoute('id')
            ));
            $viewModel->setTemplate('resources/additional/add');
            return $viewModel;
    
        }else {
            return $this->redirect()->toRoute('Login');
        }
    }
    
    public function emailAction()
    {
        $this->layout()->setTemplate('layout/additionallayout');
    
        if ($this->getAuthService()->getIdentity()) {
            return $this->redirect()->toUrl('http://www.gmail.com');
        }else {
            return $this->redirect()->toRoute('Login');
        }
    }
    
    
}